var doms = {
    modal: document.getElementById('modal'),
    alertModal: document.getElementById('alert-modal'),
    helper: document.getElementById('helper'),
    giftPool: document.getElementById('giftpool'),
    priseRecord: document.getElementById('prise-record'),
    prise: document.getElementById('prise'),
    rank: document.getElementById('rank'),
    hammer: document.getElementById('hammer'),
    eggRift: document.getElementById('egg-rift'),
    perchase: document.getElementById('perchase'),
    noHammer: document.getElementById('no-hammer'),
    perchaseFail: document.getElementById('perchase-fail'),
    success: document.getElementById('success'),
    perchaseNum: document.getElementById('perchase-num'),
    successEgg: document.getElementById('successEgg'),
};

function showModal(type) {
    doms.modal.style.visibility = 'visible';
    modalToggle(type, 'block');
}
function showAlert(type) {
    doms.alertModal.style.visibility = 'visible';
    modalToggle(type, 'block');
}
function closeModal(type) {

    doms.modal.style.visibility = 'hidden';
    modalToggle(type, 'none');
}
function closeAlert(type) {

    doms.alertModal.style.visibility = 'hidden';
    modalToggle(type, 'none');
}
function modalToggle(type, style) {

    switch (type) {
        case 'help':
            doms.helper.style.display = style;
            break;
        case 'successEgg':
            doms.successEgg.style.display = style;
            break;
        case 'giftpool':
            doms.giftPool.style.display = style;
            break;
        case 'success':
            doms.success.style.display = style;
            break;
        case 'priseRecord':
            if (style == 'none'){
                doms.eggRift.style.display = style;
            }
            doms.priseRecord.style.display = style;
            break;
        case 'prise':
            if (style == 'block'){
                //查询中奖记录
                $.ajax({

                    type: "post",

                    url: info.prise,

                    cache:false,

                    async:false,

                    data : {'token':info.hash},

                    success: function(data){

                        if (data.code == 1){
                           var str = '';
                           $.each(data.data,function (k,v) {
                               str += '<li>\n' +
                                   '                    <img src="'+v.img+'" alt="">\n' +
                                   '                    <span>'+v.name+'('+v.price+'钻)</span>\n' +
                                   '                    <span class="prise-num">X'+v.num+'</span>\n' +
                                   '                </li>';
                           });
                           $("#list2").html(str);
                        }else {
                            alert(data.msg);
                            return false;
                        }

                    }

                });
            }
            doms.prise.style.display = style;
            break;
        case 'rank':
            doms.rank.style.display = style;
            break;
        case 'perchase':
            if (style == 'block'){

                var num = $('.hits-selected').data('num');
                $("#perchase-num").val(num);
                $("#all").html(info.hammerPrice*num);
            }


            doms.perchase.style.display = style;
            break;
        case 'noHammer':
            doms.noHammer.style.display = style;
            break;
        case 'perchaseFail':
            doms.perchaseFail.style.display = style;
            break;
    }
}
function dayAndWeekSelect(btn) {
    var id = $(btn).data('id');
    $('.dayClass').css('display','none');
    $("#"+id).css('display','block');
    var last = btn.previousElementSibling || btn.nextElementSibling;
    last.classList.remove('dayandweek-selected');
    btn.classList.add('dayandweek-selected');
}
function hitsSelect(btn) {
    info.num = $(btn).data('num');
    var children = btn.parentNode.children;
    for (var i = 0, len = children.length; i < len; i++) {
        children[i].classList.remove('hits-selected');
    }
    btn.classList.add('hits-selected');
}
doms.hammer.addEventListener('animationiteration', function (e) {

    hammer.classList.remove('animate');

    if (!$('.show').hasClass('checked')){
        showModal('priseRecord');
    }

    $('.hit-me').attr('disabled', false);
});

doms.hammer.addEventListener('animationstart', function (e) {

    setTimeout(function () {
        doms.eggRift.style.display = 'block';
        $('.hits-selected').click();
    }, 700);

});
var timer = setInterval(function () {
    if ($('.hit-me').attr('disabled') == 'disabled'){
        console.log($('.hammer').stop());
    }
},500);
/**
 * 购买锤子
 */
function buyHammer() {
    var balance = $("#balance").html();
    if (!balance || balance <= 0){
        showAlert('perchaseFail');
        return false;
    }else {

        var num = $("#perchase-num").val();

        $.ajax({

            type: "post",

            url: info.buyUrl,

            cache:false,

            async:false,

            data : {'token':info.hash,'num':num},

            success: function(data){
                closeModal('perchase');
                if (data.code == 1){
                    $("#num").html(num);
                    var hammerNum = $("#hammerNum").html();

                    var newNum = parseInt(hammerNum) + parseInt(num);
                    $("#hammerNum").html(newNum);

                    $("#balance").html(data.data.total);
                    showAlert('success');
                    return false;
                }

                if (data.code == 0){
                    var msg = '<p class="alert-content">'+data.msg+'</p>'+
                        '<p class="alert-content">请稍后重试！</p>'
                }else{
                    var msg = '<p class="alert-content">您的余额不足哦！</p>\n' +
                        '            <p class="alert-content">充值后再来购买金幸运锤吧！</p>';
                }
                $("#error").html(msg);
                showAlert('perchaseFail');
            }

        });

    }

}

/**
 * 砸蛋
 * @returns {boolean}
 */
function hitEgg(obj) {

    var hammerNum = $("#hammerNum").html();

    var num = info.num;
    if ($(obj).attr('disabled') == 'disabled' || $('#prise-record').css('display') == 'block' ){
        return false;
    }

    if (!hammerNum || hammerNum <= 0 || hammerNum < num){
        showAlert('noHammer');
        return false;
    }

    $(obj).attr('disabled', 'disabled');
    doms.eggRift.style.display = 'none';

    $.ajax({

        type: "post",

        url: info.smash,

        cache:false,

        async:false,

        data : {'token':info.hash,'num':num,'room_id':info.room_id},

        success: function(data){

            if (data.code == 1){
                if($('.show').hasClass('checked')){
                    // alert('砸蛋成功');
                    // return false;
                }else {
                    if (num == 10){
                        var lian = '十连砸';
                    }else if(num == 100){
                        var lian = '暴击';
                    }else{
                        var lian = '砸蛋';
                    }

                    $("#lian").html(lian);

                    var str = '';
                    $.each(data.data,function (k,v) {
                        str +=
                            '<li><img src="'+v.img+'" alt="">\n' +
                            '                    <span>'+v.name+'('+v.price+'钻)</span>\n' +
                            '                    <span class="prise-num">X'+v.num+'</span>\n' +
                            '                </li>'
                    });
                    $("#list").html(str);
                }


                var hammerNum = $("#hammerNum").html();

                var newNum = parseInt(hammerNum) - num;

                $("#hammerNum").html(newNum);

                doms.hammer.classList.add('animate');

            }else {
                $(obj).attr('disabled', false);
                alert(data.msg);

                return false;
            }
        }

    });

}
function radioClick(radio) {
    var classList = radio.children[0].classList;
    if (classList.value.indexOf('checked') > -1) {
        classList.remove('checked');
    } else {
        classList.add('checked');
    }
}
function perchaseNumSet(dir) {
    var num = Number(doms.perchaseNum.value);
    num += dir;
    if (num < 1) {
        return;
    }
    doms.perchaseNum.value = num;
    var all = parseInt(num) * info.hammerPrice;
    $("#all").html(all);

}

$('#perchase-num').keyup(function () {
    var num = Number(doms.perchaseNum.value);

    if(num && num <= 1){
        num = 1;
        doms.perchaseNum.value = num;
    }

    var all = parseInt(num) * info.hammerPrice;
    $("#all").html(all);
});
